// pages/account/index.js
var app = getApp(), fetchApi = require("../../utils/httpPost.js");
import { hexMD5 } from '../../utils/md5.js';
import Toast from '../../component/vant/toast/toast';
import Dialog from '../../component/vant/dialog/dialog';
const CryptoJS = require("../../utils/aesUtil.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNum:'去绑定',
    user:{},
    action:'',
    from_type:'',
    tbUserNo:'',
    showTbUserNoDialog:false,
    is_has_pay_pwd:0,
    payPwd:'',
    code:'',
    nickName:'',
    faceUrl:'',
    user_id:0,
    sendCodeState:0,
    isShowPop:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('account options')
    if(options.from_type){
      this.data.from_type = options.from_type;
    }
    this.data.user_id = app.getStorageSync("user_id");
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/wxUser/getAccountInfo', {}, function (res) {
      console.log("账号绑定 数据", res)
      if(res.data.code == 0){
        that.data.phoneNum = res.data.data.phone_num;
        that.setData({
          is_has_pay_pwd: res.data.data.is_has_pay_pwd,
          phoneNum: res.data.data.phone_num!=undefined &&  res.data.data.phone_num!='' ? res.data.data.phone_num : '去绑定',
          nickName: res.data.data.nick_name,
          faceUrl:  res.data.data.face_url,
        })
      }
      that.setData({
        netWork: true,
      })
    }, function () {
        that.setData({ netWork: false, hasData: false })
    })
  },
  getPhoneNumber(e){
    console.log('getPhoneNumber',e)
    var that = this;
    fetchApi.httpPost('/wxUser/getWxPhoneNum', { 
      code: e.detail.code, 
      user_type : 0,
    }, function (res) {
        if(res.data.code == 0){
          Toast.success(res.data.msg);
          app.setStorageSync("phone_num",res.data.data);
          that.setData({
            phoneNum: res.data.data
          })
        }else{
          Toast.fail(res.data.msg)
        }
    });
  },
  onShow(options){
    this.onHttpInit();
  },
  onClickTbUserNo(e){
    this.setData({
      showTbUserNoDialog:true
    })
  },
  onChooseAvatar(e){
    var that = this;
    const { avatarUrl } = e.detail ;
    console.log('avatarUrl',avatarUrl);
    if(avatarUrl!=undefined && avatarUrl !=''){
      wx.uploadFile({
        url: fetchApi.getPlatformUrl() + '/wxUser/updateFaceUrl',
        filePath: avatarUrl,
        name: 'file',
        formData: { user_id: that.data.user_id },
        success: function (res) {
          console.log('uploadFile res', res)
          var jsonData = JSON.parse(res.data);
          console.log('jsonData',jsonData)
          if(jsonData.code == 0){
            Toast.success('上传成功');
            that.setData({
              faceUrl: jsonData.data,
            })
            app.setStorageSync("face_url",jsonData.data);
          }else{
            Toast.fail('上传失败');

          }
        }, fail: function (res) {
          Toast.fail('上传失败');
          console.log('uploadFile fail res', res)
        }
      })
    }
  },
  uploadNickName(){
    this.setData({
      isShowPop : !this.data.isShowPop,
    })
  },
  inputNickName(e){
    var nickName = e.detail;
    this.data.nickName = nickName;
  },
  goUpdateNickName(){
    if(this.data.nickName == ''){
      Toast("请输入昵称");
      return;
    }

    var that = this;
    fetchApi.httpPost('/wxUser/updateNickName', {
      nickName: that.data.nickName,
    }, function (res) {
      console.log("修改昵称 数据", res)
      if(res.data.code == 0){
        Toast.success(res.data.msg);
        that.setData({
          nickName: that.data.nickName,
          isShowPop : false,
        })
        app.setStorageSync("nick_name",that.data.nickName);
      }else{
        Toast.fail(res.data.msg);
      }
    }, function () {
    })
  },
  goSetting(){
    wx.navigateTo({
      url: '/pages/account/pwd/index',
    })
  },
  tbUserNoInput(e){
    this.data.tbUserNo = e.detail;
  },
  payPwdInput(e){
    // 统一使用MD5加密
    var ps = hexMD5(e.detail);
    // this.data.payPwd = CryptoJS.Encrypt(e.detail);
    this.data.payPwd = ps;
    
  },
  codeInput(e){
    this.data.code = e.detail;
  },
  onHttpSendCode(e){
    var that = this;
    fetchApi.httpPost('/wxUser/sendCode', {
      phone_num:that.data.phoneNum,
      code_type:0,
    }, function (res) {
      console.log("发送验证码", res)
      if(res.data.code == 0){
        Toast.success(res.data.msg)
        that.setData({
          sendCodeState:1
        })
      }else{
        Toast.fail(res.data.msg)
      }
    }, function () {
        that.setData({ netWork: false, hasData: false })
    })
  },
  onCloseDialog(e){
    console.log('onCloseDialog',e)
    var type = e.detail;
    if(type == 'confirm'){

      if(this.data.payPwd == ''){
        Toast.fail("请输入支付密码");
        return ;
      }
      if(this.data.code == ''){
        Toast.fail("请输入验证码");
        return ;
      }

      this.onHttpUpdatePwd();
    }
  },
  onHttpUpdatePwd(){
    var that = this;
    fetchApi.httpPost('/wxUser/updatePayPwd', {
      phone_num:that.data.phoneNum,
      pwd: that.data.payPwd,
      code:that.data.code,
    }, function (res) {
      console.log("修改密码", res)
      if(res.data.code == 0){
        Toast.success(res.data.msg)
      }else{
        Toast.fail(res.data.msg)
      }
    }, function () {
        that.setData({ netWork: false, hasData: false })
    })
  },
  onDialogTbUserNoClose(e){
    var that = this
    fetchApi.httpPost('/wxUser/bandTbUserNo', {
      user_no:that.data.tbUserNo,
    }, function (res) {
      console.log("账号绑定数据", res)
      if(res.data.code == 0){
        var user = res.data.data;
        that.setData({
          user:user,
          showTbUserNoDialog:false,
        })
        Toast.success(res.data.msg)

      }else{
        Toast.fail(res.data.msg)
      }
    }, function () {
        that.setData({ netWork: false, hasData: false })
    })
  }
})